package com.ledim.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.ledim.fragment.base.LedimBaseDialogFragment;
import com.ledim.itf.LedimDialogClickListner;
import com.ledim.utils.ConstantUtil;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/9/13.
 * 目前乐次元用到的弹出dialog，样式分按钮垂直或者水平分布2种
 */
public class CommonDialogFragment extends LedimBaseDialogFragment {

    private String mTitle;
    private String mPositiveStr;
    private String mNegativeStr;
    private int mType;
    private boolean mCanCanceledOnTouchOutside = true;
    private void setClickListner(LedimDialogClickListner mClickListner) {
        this.mClickListner = mClickListner;
    }

    private LedimDialogClickListner mClickListner;

    public static CommonDialogFragment getInstance(int type, String title, String yes, String no,
                                                   LedimDialogClickListner clickListner) {
        CommonDialogFragment fragment = new CommonDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ConstantUtil.DialogConstant.COMMONTYPE, type);
        bundle.putString(ConstantUtil.DialogConstant.POSITIVE, yes);
        bundle.putString(ConstantUtil.DialogConstant.NEGATIVE, no);
        bundle.putString(ConstantUtil.DialogConstant.TITLE, title);
        fragment.setArguments(bundle);
        fragment.setClickListner(clickListner);
        return fragment;
    }

    public static CommonDialogFragment getInstance(int type, String title, String yes, String no,
                                                   LedimDialogClickListner clickListner,
                                                   boolean canCanceledOnTouchOutside) {
        CommonDialogFragment fragment = getInstance(type, title, yes, no, clickListner);
        fragment.getArguments()
                .putBoolean(ConstantUtil.DialogConstant.CANCEL, canCanceledOnTouchOutside);
        return fragment;
    }

    protected boolean canCanceledOnTouchOutside() {
        return mCanCanceledOnTouchOutside;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mType = bundle.getInt(ConstantUtil.DialogConstant.COMMONTYPE);
            mTitle = bundle.getString(ConstantUtil.DialogConstant.TITLE);
            mPositiveStr = bundle.getString(ConstantUtil.DialogConstant.POSITIVE);
            mNegativeStr = bundle.getString(ConstantUtil.DialogConstant.NEGATIVE);
            mCanCanceledOnTouchOutside =
                    bundle.getBoolean(ConstantUtil.DialogConstant.CANCEL, true);
        }
    }

    @Override
    public int getLayoutId() {
        return mType == ConstantUtil.DialogConstant.DIALOG_HORIZONTAL_TYPE
               ? R.layout.dialog_layout_horizontal : R.layout.dialog_layout_vertical;
    }

    @Override
    public void initView(View view) {
        TextView titleTv = (TextView) view.findViewById(R.id.dialog_message);
        titleTv.setText(mTitle);
        TextView positiveTv = (TextView) view.findViewById(R.id.yes);
        if (TextUtils.isEmpty(mPositiveStr)) {
            positiveTv.setVisibility(View.GONE);
        } else {
            positiveTv.setText(mPositiveStr);
        }
        positiveTv.setOnClickListener(this);
        TextView negativeTv = (TextView) view.findViewById(R.id.no);
        if (TextUtils.isEmpty(mNegativeStr)) {
            negativeTv.setVisibility(View.GONE);
        } else {
            negativeTv.setText(mNegativeStr);
        }
        negativeTv.setOnClickListener(this);
    }

    @Override
    public void initLayoutParams(WindowManager.LayoutParams wlp) {
        wlp.gravity = Gravity.CENTER;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        dismiss();
        if (mClickListner != null) {
            switch (v.getId()) {
                case R.id.yes:
                    mClickListner.onClickPositive(v);
                    break;
                case R.id.no:
                    mClickListner.onClickNegative(v);
                    break;
            }
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mClickListner != null) {
            mClickListner.onDismiss();
        }
    }
}
